package com.example.api.controller;

import com.example.api.aspect.SystemControllerLog;
import com.example.api.model.constants.LogisticsConstants;
import com.example.api.model.dto.LoginDto;
import com.example.api.model.entity.Admin;
import com.example.api.model.enums.ActionTypeEnum;
import com.example.api.model.support.RespBean;
import com.example.api.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(tags = "登录管理")
@RestController
public class LoginController {

    @Resource
    private AdminService adminService;

    @ApiOperation("是否初始化")
    @GetMapping("/hasInit")
    public RespBean hasInit() {
        return adminService.selectByRoleId(LogisticsConstants.ROLE_ID_1);
    }

    @ApiOperation("用户初始化")
    @PostMapping("/init")
    public RespBean init(@RequestBody Admin admin) {
        return adminService.init(admin);
    }

    @ApiOperation("登录")
    @PostMapping("/login")
    @SystemControllerLog(description = "用户成功登录", actionType = ActionTypeEnum.OPERATE)
    public RespBean login(@RequestBody LoginDto dto) throws Exception {
        return adminService.login(dto);
    }

    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    @SystemControllerLog(description = "用户安全退出系统", actionType = ActionTypeEnum.OPERATE)
    public RespBean logout() {
        return RespBean.success("注销成功！");
    }

    @ApiOperation("发送邮件")
    @GetMapping("/send-email")
    @SystemControllerLog(description = "用户发送邮件", actionType = ActionTypeEnum.OPERATE)
    public RespBean sendEmail(String email) {
        return adminService.sendEmail(email);
    }

}